package cn.fudong.bos.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.fudong.bos.dao.base.CourierRepository;
import cn.fudong.bos.dao.base.FixedAreaRepository;
import cn.fudong.bos.dao.base.TakeTimeRepository;
import cn.fudong.bos.domain.base.Courier;
import cn.fudong.bos.domain.base.FixedArea;
import cn.fudong.bos.domain.base.TakeTime;
import cn.fudong.bos.service.base.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

	@Autowired
	private FixedAreaRepository fixedAreaRepository;

	// 保存操作
	@Override
	public void save(FixedArea FixedArea) {

		fixedAreaRepository.save(FixedArea);
	}

	// 查询全部
	@Override
	public Page<FixedArea> findPageData(Pageable pageable, Specification<FixedArea> specification) {

		return fixedAreaRepository.findAll(specification, pageable);
	}

	@Autowired
	private CourierRepository courierRepository;

	@Autowired
	private TakeTimeRepository takeTimeRepository;

	// 管理快递员到定区
	@Override
	public void associationCourierToFixedArea(FixedArea fixedArea, Integer courierId, Integer takeTimeId) {
		
		FixedArea persistfixedArea = fixedAreaRepository.findOne(fixedArea.getId());
		Courier courier = courierRepository.findOne(courierId);
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		//快递员关联到定区
		persistfixedArea.getCouriers().add(courier);
		//收派时间关联快递员	
		courier.setTakeTime(takeTime);
	}

}